package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Link;
import com.ws.lv.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/link")
public class LinkController {
    @Autowired
    LinkService linkService;

    //去友情链接列表
    @RequestMapping("/selectLink")
    public String selectLinkAll() {
        return "adminpage/link/list";
    }

    //去添加友情链接页面
    @RequestMapping("/toAddLink")
    public String toAddLink() {
        return "adminpage/link/add";
    }

    //去修改友情链接页面
    @RequestMapping("/toUpdateLink")
    public String toUpdateLink(int link_id, Model model) {
        System.out.println("去修改友情链接页面");
        Link link = linkService.selectLinkById(link_id);
        model.addAttribute("link", link);
        return "adminpage/link/update";
    }

    //查询友情链接
    @RequestMapping("/selectLinks")
    public String selectLinks(Model model, String link_name, Integer link_state, String link_order,Integer currentPage) {
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Link> links = linkService.selectLinks(link_name, link_state, link_order);
        PageInfo pageInfo = new PageInfo(links);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("link_name", link_name);
        model.addAttribute("link_state", link_state);
        model.addAttribute("link_order", link_order);
        return "adminpage/link/list";
    }

    //添加友情链接
    @RequestMapping("/insertLink")
    public String insertLink(Link link) {
        int i = linkService.insertLink(link);
        return "redirect:/link/selectLinks";
    }

    //显示友情链接
    @RequestMapping("/updateLinkDisplay")
    public String updateLinkDisplay(int link_id) {
        int i = linkService.updateLinkDisplay(link_id);
        return "forward:/link/selectLinks";
    }

    //隐藏友情链接
    @RequestMapping("/updateLinkHide")
    public String updateLinkHide(int link_id) {
        int i = linkService.updateLinkHide(link_id);
        return "forward:/link/selectLinks";
    }

    //删除友情链接
    @RequestMapping("/deleteLinks")
    public String deleteLinks(String link_ids) {
        String[] ids = link_ids.split(",");
        ArrayList<Integer> link_id = new ArrayList<>();
        for (String id : ids) {
            link_id.add(Integer.parseInt(id));
        }
        int i = linkService.deleteLinks(link_id);
        return "redirect:/link/selectLinks";
    }

    //修改友情链接
    @RequestMapping("/updateLink")
    public String updateLink(Link link) {
        int i = linkService.updateLink(link);
        return "redirect:/link/selectLinks";
    }


}
